#region (c) 2009 Abdullin.com and Contributors

// Copyright (c) 2009 Abdullin.com and Contributors
// Site: http://abdullin.com/ncouch
// This code is released under the terms of the new BSD licence

#endregion

using System.IO;
using NCouch.Model;
using NCouch.Rest;

namespace NCouch.Error
{
	public static class CouchError
	{
		public static CouchException Generic(ErrorInfo info)
		{
			return new CouchException(info.Reason);
		}


		public static CouchException Unexpected(IRestResponse restResponse)
		{
			var s = string.Empty;
			restResponse.RunReader(stream => s = ReadStream(stream));

			var txt = string.Format("Unexpected return\nError code:{0}\nStream:{1}", restResponse.StatusCode, s);
			return new CouchException(txt);
		}

		public static CouchException DatabaseAlreadyExists(IRestResponse response)
		{
			return new CouchException("Database already exists");
		}

		static string ReadStream(Stream stream)
		{
			using (var reader = new StreamReader(stream))
			{
				return reader.ReadToEnd();
			}
		}
	}
}